#pragma once

#include <iostream>
#include <string>
#include <memory>
#include <jsoncpp/json/json.h>

const std::string Sep = "\r\n"; // 回车换行符

// 封装: {json} -> len\r\n{json}\r\n
bool Encode(std::string &message)
{
    if (message.size() == 0)
        return false;
    // 添加报头, 形成完整的报文
    std::string package = std::to_string(message.size()) + Sep + message + Sep;
    message = package;

    return true;
}

// 解包: len\r\n{json}\r\n -> {json}
// 同时需要保证报文的完整性！
// 123\r\n
// 123\r\n{json}
// 123\r\n{json}\r\n
// 123\r\n{json}\r\n123\r\n{json}\r\n
bool Decode(std::string &package, std::string *content)
{
    auto pos = package.find(Sep);
    if (pos == std::string::npos)
        return false;

    std::string content_len_str = package.substr(0, pos);
    int content_len = std::stoi(content_len_str);

    // 完整报文的长度
    int full_len = content_len_str.size() + content_len + 2 * Sep.size();

    if (package.size() < full_len)
        return false;

    // {json}字符串的长度
    *content = package.substr(pos + Sep.size(), content_len);

    package.erase(0, full_len);
    return true;
}

class Request
{
public:
    Request()
        : _x(0), _y(0), _oper(0)
    {
    }
    Request(int x, int y, char oper)
        : _x(x), _y(y), _oper(oper)
    {
    }

    // 序列化
    bool Serialize(std::string &out_string)
    {
        Json::Value root;
        root["x"] = _x;
        root["y"] = _y;
        root["oper"] = _oper;

        Json::StreamWriterBuilder wb;
        std::unique_ptr<Json::StreamWriter> w(wb.newStreamWriter());
        std::stringstream ss;
        w->write(root, &ss);
        out_string = ss.str();

        return true;
    }

    // 反序列化
    bool Deserialize(std::string &in_string)
    {
        Json::Value root;
        Json::Reader reader;
        bool parsingSuccessful = reader.parse(in_string, root);
        if (!parsingSuccessful)
        {
            std::cout << "Failed to parse JSON: " << reader.getFormattedErrorMessages() << std::endl;
            return false;
        }

        _x = root["x"].asInt();
        _y = root["y"].asInt();
        _oper = root["oper"].asInt();

        return true;
    }

    void Print()
    {
        std::cout << "x: " << _x << std::endl;
        std::cout << "oper: " << _oper << std::endl;
        std::cout << "y: " << _y << std::endl;
    }

    int X() const { return _x; }
    int Y() const { return _y; }
    char Oper() const { return _oper; }

private:
    int _x;
    int _y;
    char _oper;
};

class Response
{
public:
    Response()
        : _result(0), _code(0)
    {
    }
    Response(int result, int code)
        : _result(result), _code(code)
    {
    }

    // 序列化
    bool Serialize(std::string &out_string)
    {
        Json::Value root;
        root["result"] = _result;
        root["code"] = _code;

        Json::StreamWriterBuilder wb;
        std::unique_ptr<Json::StreamWriter> w(wb.newStreamWriter());
        std::stringstream ss;
        w->write(root, &ss);
        out_string = ss.str();

        return true;
    }

    // 反序列化
    bool Deserialize(std::string &in_string)
    {
        Json::Value root;
        Json::Reader reader;
        bool parsingSuccessful = reader.parse(in_string, root);
        if (!parsingSuccessful)
        {
            std::cout << "Failed to parse JSON: " << reader.getFormattedErrorMessages() << std::endl;
            return false;
        }

        _result = root["result"].asInt();
        _code = root["code"].asInt();

        return true;
    }

    void Print()
    {
        std::cout << "result: " << _result << std::endl;
        std::cout << "code: " << _code << std::endl;
    }

    int Result() const { return _result; }
    int Code() const { return _code; }
    void SetResult(int result) { _result = result; }
    void SetCode(int code) { _code = code; }

private:
    int _result; // 结果
    int _code;   // 错误码
};